package cn.iocoder.yudao.module.pay.controller.app.point;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.pay.convert.point.PayPointRechargeConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType;

import cn.iocoder.yudao.module.pay.controller.app.point.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.point.PayPointRechargeDO;
import cn.iocoder.yudao.module.pay.service.point.PayPointRechargeService;

@Tag(name = "用户 APP - 会员钻石充值")
@RestController
@RequestMapping("/pay/point-recharge")
@Validated
public class AppPayPointRechargeController {

    @Resource
    private PayPointRechargeService pointRechargeService;

    @Resource
    private PayOrderService payOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建会员钻石充值")
    @PreAuthenticated
    public CommonResult<AppPayPointRechargeCreateRespVO> createPointRecharge(
            @Valid @RequestBody  AppPayPointRechargeCreateReqVO reqVO) {
        PayPointRechargeDO pointRecharge = pointRechargeService.createPointRecharge(
                getLoginUserId(), getLoginUserType(), getClientIP(), reqVO);
        return success(PayPointRechargeConvert.INSTANCE.convert(pointRecharge));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员钻石充值分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppPayPointRechargeRespVO>> getPointRechargePage(@Valid AppPayPointRechargePageReqVO pageReqVO) {
        PageResult<PayPointRechargeDO> pageResult = pointRechargeService.getPointRechargePage(
                getLoginUserId(), UserTypeEnum.MEMBER.getValue(), pageReqVO, true);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        // 拼接数据
        List<PayOrderDO> payOrderList = payOrderService.getOrderList(
                convertList(pageResult.getList(), PayPointRechargeDO::getPayOrderId));
        return success(PayPointRechargeConvert.INSTANCE.convertPage(pageResult, payOrderList));
    }

}